/**
 * Created by zzy on 2016/12/8.
 */
var $ = require('jquery');
require('easyui');
require('layout/uiJs');
require('layout/util');
var $alert = require("libs/alert");
require('js-path/jquery-validation-wrapper');
var isSale = require('libs/user-type').isSale;
$(function () {
    $('#search').on('click',function () {
        // var c = $('#country_id').combobox('getValue');
        $('#warehouseTable').datagrid('load',$.extend(
            $('#search_form').serializeObject(),
            {
                countryName: $('#country_id').combobox('getValue'),
                fromDate: $('#startDt').data('utc0'),
                toDate: $('#endDt').data('utc0')
            }
        ))
    });
    $(document).on('click','a[name=warehouseDetail]',function () {
        window.parent.tabOpen($(this).attr('ref'),'仓库详情',true,true);
    });
    $('#country_id').combobox({
        mode:'remote',
        url:'api/v1/p/crm/area/queryCode',
        editable:true,
        hasDownArrow:false,
        valueField: 'name',
        textField: 'name',
        onBeforeLoad: function(param){
            param.parentId = 0;
            param.level = 1;
            param.subCode = param.q;
            /*if(param == null || param.q == null || param.q.replace(/ /g, '') == ''){
                var value = $(this).combobox('getValue');
                if(value){
                    param.id = value;
                    return true;
                }
                return false;
            }*/
        },
        /*onSelect: function(param){
            $('#country_id').data('id',param.id);
        },*/
        loadFilter:function(result){
            if(result.code == '200'){
                return result.data;
            }
        }
    });
    tableInit();
    function tableInit() {
        $('#warehouseTable').datagrid({
            url:'api/v1/p/mms/warehouse/list',
            border : false,
            height:$(window).height(),
            pagination : true,
            pageSize : 20,
            pageList : [ 20, 50,  100 ],
            emptyMsg : "暂无数据",
            singleSelect: true,
            rownumbers:true,
            columns:[[
                {
                    field:'warehouseName',
                    title:'仓库名称',
                    width:'15%',
                    formatter:function (value,row,index) {
                        return "<a class='ellipsis' href='javascript:;' ref='warehouse-detail?id="+row.warehouseId+"&type="+row.type+"' name='warehouseDetail' title='"+value+"'>"+value+"</a>";
                    }
                },{
                    field:'countryName',
                    title:'国家/地区',
                    width:'10%',
                    formatter:function (value,row,index) {
                        return "<div class='ellipsis' title='"+value+"'>"+value+"</div>";
                    }
                },
                {
                    field:'cityName',
                    title:'城市',
                    width:'10%',
                    formatter:function (value,row,index) {
                        return "<div class='ellipsis' title='"+value+"'>"+value+"</div>";
                    }
                },{
                    field:'zipCode',
                    title:'邮编',
                    width:'8%',
                    formatter:function (value,row,index) {
                        return "<div class='ellipsis' title='"+value+"'>"+value+"</div>";
                    }
                },{
                    field:'address',
                    title:'仓库地址',
                    width:'16%',
                    formatter:function (value,row,index) {
                        return "<div class='ellipsis' title='"+value+"'>"+value+"</div>";
                    }
                },{
                    field:'timeZone',
                    title:'时区',
                    width:'8%',
                    formatter:function (value,row,index) {
                        return "<div class='ellipsis' title='"+value+"'>"+value+"</div>";
                    }
                },{
                    field:'createDt',
                    title:'创建时间',
                    width:'15%',
                    formatter:function (value,row,index) {
                        return "<div class='ellipsis' title='"+value+"'>"+value+"</div>";
                    }
                },{
                    field:'type',
                    title:'类型',
                    width:'10%',
                    formatter:function (value,row,index) {
                        switch (value){
                            case 'C': return '集货仓';
                            case 'P': return '港口仓';
                            case 'O': return '海外仓';
                            case 'CP': return '集货仓港口仓';
                            default: return '';
                        }
                    }
                },{
                    field:'opt',
                    title:'操作',
                    hidden:isSale,
                    width:'8%',
                    formatter:function (value,row,index) {
                        return '';
                    }
                }
            ]],
            loadFilter:function(data){
                if(data.code == '200'){
                    return data.data;
                }else{
                    return [];
                }
            },
            toolbar:"#tb"
        });
    }
});
